package history.tencent1.problem3;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 *
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int arrayCount = sc.nextInt();
        sc.nextLine();
        List<List<Integer>> arrays = new ArrayList<>();
        while(arrayCount -- > 0){
            List<Integer> tempArray = new ArrayList<>();
            String[] elements = sc.nextLine().split(" ");
            System.out.println(Arrays.toString(elements));
            for(String element: elements){
                tempArray.add(Integer.parseInt(element));
            }
            tempArray.remove(0);
            System.out.println("tempArray: " + tempArray.toString());
            arrays.add(tempArray);
        }
        int queryCount = sc.nextInt();
        sc.nextLine();
        while(queryCount -- > 0){
            int listToAdd = sc.nextInt();
            List<Integer> toSort = new ArrayList<>();
            while(listToAdd -- > 0){
                int listToAddNum = sc.nextInt();
                toSort.addAll(arrays.get(listToAddNum - 1));
            }
            toSort.sort(null);
            System.out.println(toSort.get(sc.nextInt() - 1));
        }
    }
}
